package com.yztc.orangeentertainment.ui.main.channalarticle;

import android.content.Context;

import com.yztc.orangeentertainment.bean.channalarticle.ChannalArticle;
import com.yztc.orangeentertainment.bean.channalarticle.ChannalArticleAttitudeBean;
import com.yztc.orangeentertainment.bean.channalarticle.ChannalArticleFooterBean;
import com.yztc.orangeentertainment.bean.channalarticle.ChannalArticleIMGBean;
import com.yztc.orangeentertainment.bean.channalarticle.ChannalArticleInfoBean;
import com.yztc.orangeentertainment.bean.channalarticlecomment.ArticleBean;
import com.yztc.orangeentertainment.bean.channalarticlecomment.ArticleCommentAttitudeBean;
import com.yztc.orangeentertainment.bean.channalarticlecomment.ArticleCommentBean;
import com.yztc.orangeentertainment.bean.channalarticlecomment.ArticleCommentDataBean;
import com.yztc.orangeentertainment.bean.channalarticlecomment.ChannalArticleComment;
import com.yztc.orangeentertainment.config.ChannalUrlConfig;
import com.yztc.orangeentertainment.http.ChannalRetrofitService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by My on 2016/10/10.
 */
public class ChannalArticleModle implements ChannalArticleContract.Modle {

    @Override
    public void getChannalArticle(String type, Map<String, String> map,
                                  final OnLoadChannalArticleListener loadChannalArticleListener, Context context) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ChannalUrlConfig.PATH.BASE_CHANNALARTICLE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ChannalArticle> call=retrofit.create(ChannalRetrofitService.class)
                .getChannalArticleList(type,map);
        call.enqueue(new Callback<ChannalArticle>() {
            @Override
            public void onResponse(Call<ChannalArticle> call, Response<ChannalArticle> response) {
                ChannalArticleInfoBean infoBeen=response.body().getData().getInfo();
                ChannalArticleFooterBean footerBean=response.body().getData().getFooter();
                String content=response.body().getData().getContents();
                List<String> contentList=new ArrayList<String>();
                List<ChannalArticleAttitudeBean> attitudeBeanList=response.body().getData().getFooter()
                        .getAttitude();
                String regex="<p.*?>(.*?)</p>";
                Pattern p=Pattern.compile(regex);
                Matcher m=p.matcher(content);
                while(m.find()){
                    String a=m.group();
                    contentList.add(a);
                }
                List<ChannalArticleIMGBean> imgBeanList=response.body().getData().getResources()
                        .getIMG();
                loadChannalArticleListener.onInfoSuccess(infoBeen);
                loadChannalArticleListener.onFooterlistSuccess(footerBean);
                loadChannalArticleListener.onContentlistSuccess(contentList,imgBeanList);
                loadChannalArticleListener.onAttitudelistSuccess(attitudeBeanList);
            }

            @Override
            public void onFailure(Call<ChannalArticle> call, Throwable t) {
                loadChannalArticleListener.onInfoFailure(t.getMessage());
                loadChannalArticleListener.onFooterlistFailure(t.getMessage());
                loadChannalArticleListener.onContentlistFailure(t.getMessage());
                loadChannalArticleListener.onAttitudelistFailure(t.getMessage());
            }
        });
    }

    @Override
    public void getChannalArticleFromDb(Call<ChannalArticleComment> call) {
    }

    @Override
    public void getChannalArticleComment(String type, Map<String, String> map, final
    OnLoadChannalArticleCommentListener loadChannalArticleCommentListener, Context context) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ChannalUrlConfig.PATH.BASE_CHANNALARTICLE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ChannalArticleComment> call1=retrofit.create(ChannalRetrofitService.class)
                .getChannalArticleCommentList(type,map);
        call1.enqueue(new Callback<ChannalArticleComment>() {
            @Override
            public void onResponse(Call<ChannalArticleComment> call, Response<ChannalArticleComment> response) {
                ArticleCommentDataBean dataBean=response.body().getData();
                ArticleCommentAttitudeBean attitudeBean=dataBean.getAttitude();
                ArticleBean articleBean=dataBean.getArticle();
                ArticleCommentBean commentBean=dataBean.getComment();
                loadChannalArticleCommentListener.onCommentAttitudelistSuccess(attitudeBean,articleBean);
                loadChannalArticleCommentListener.onCommentBeanSuccess(commentBean);
            }

            @Override
            public void onFailure(Call<ChannalArticleComment> call, Throwable t) {
                loadChannalArticleCommentListener.onCommentAttitudelistFailure(t.getMessage());
                loadChannalArticleCommentListener.onCommentBeanFailure(t.getMessage());
            }
        });
    }

    @Override
    public void getChannalArticleCommentFromDb(Call<ChannalArticle> call) {

    }
}